package com.xbdun.web.validatebmp.gataway;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.xbdun.ValidateWebApplication;
import com.xbdun.common.api.XbdUnObjectResult;
import com.xbdun.common.enums.XbdunPkgEnum;
import com.xbdun.common.exceptions.BizException;
import com.xbdun.common.util.CommonUtil;
import com.xbdun.common.util.MacUtil;
import com.xbdun.facade.validatebmp.exceptions.ValidateBizException;
import com.xbdun.web.validatebmp.frame.annotation.PayPathConfig;
import com.xbdun.web.validatebmp.frame.gateway.AbWayService;
import com.xbdun.web.validatebmp.request.bmp.GeneroBmpQrRequest;
import com.xbdun.web.validatebmp.response.GeneroQrWayResponse;
import com.xbdun.web.validatebmp.service.QrCodeService;
import com.xbdun.web.validatebmp.utils.qr.QRCodeUtil;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
@PayPathConfig(RegisterMethodEnum.GENRO_PERSION_QR)
public class GeneroQrWayService extends AbWayService<GeneroBmpQrRequest> {
	@Autowired
	QrCodeService qrCodeService;
	@Value("${xbdun.open.shopbp.protocol}")
	private String shopProtol;//商城开放平台地址
	@Override
	public void doValidate(GeneroBmpQrRequest model, StringBuilder refMsg) {
		if(StringUtils.isBlank(model.getScanQrValue())){
			if(!NumberUtils.isDigits(model.getOrgCode())){
				refMsg.append("机构编号不能为空;");
			}
			if(StringUtils.isBlank(model.getWxUnionId())&&StringUtils.isBlank(model.getUserCode())){
				refMsg.append("微信UniontId与用户编号不能同时为空;");
			}
			if(StringUtils.isBlank(model.getRpcMethod())){
				refMsg.append("远程调用方法参数不能为空[rpcMethod];");
			}
		}
		super.doValidate(model, refMsg);
	}
	
	public XbdUnObjectResult doProcess(HttpServletRequest requst, HttpServletResponse response,GeneroBmpQrRequest model, XbdunPkgEnum xbdunPkgEnum) throws BizException {
		XbdUnObjectResult result = null;
		try{
			if(StringUtils.isBlank(model.getScanQrValue())){
				//如果不是客户，那么生成的二维码肯定不应该将用户的微信UnionId加入
				if(!XbdunPkgEnum.CUSTOMER.equals(xbdunPkgEnum)){
					model.setWxUnionId(StringUtils.EMPTY);
					if(NumberUtils.toLong(model.getUserCode())==0){
						result = XbdUnObjectResult.newInstance();
						result.setBizException(ValidateBizException.QR_GENRO_FAIL.newInstance("用户编号不能为空"));
						return result;
					}
				}
				GeneroQrWayResponse wxMac = new GeneroQrWayResponse();
				
				wxMac.setSuCde(model.getUserCode());
				wxMac.setSuUId(model.getWxUnionId());
				wxMac.setSuOgCde(model.getOrgCode());
				wxMac.setPkgName(xbdunPkgEnum.getKey());
				wxMac.setTimestamp(System.currentTimeMillis());
				wxMac.setMethod(model.getRpcMethod());//远程调用方法
				
				MacUtil.encryption(wxMac,xbdunPkgEnum.getAccount(), xbdunPkgEnum.getSecritVal());
				String createLinke = MacUtil.createLinkString(wxMac);
				//如果二维码生成为空，那么说明生成失败
				if(StringUtils.isBlank(createLinke)){
					result = XbdUnObjectResult.newInstance();
					result.setBizException(ValidateBizException.QR_GENRO_FAIL);
					return result;
				}
				
				//组装字二维码字符串
				StringBuilder scanQrStr = new StringBuilder(CommonUtil.PROTOCOL);
				scanQrStr.append(ValidateWebApplication.OPEN_SHOP_GATAWAY_URL);
				scanQrStr.append("?"+createLinke);
				
				createLinke = null;
				model.setScanQrValue(scanQrStr.toString());
				scanQrStr.setLength(0);
				scanQrStr = null;
			}
			byte[] encodeByte = qrCodeService.encodeByte(model.getUrl(),model.getScanQrValue(), model.getNeedIoc(), model.getQrSize(), model.getHeight(), model.getWidth());
			QRCodeUtil.wirteOut(encodeByte,response.getOutputStream());	
		}catch(Exception e){
			log.error("生成二维码失败",e);
			result = XbdUnObjectResult.newInstance();
			result.setBizException(ValidateBizException.QR_GENRO_FAIL);
		}
		return result;
	}
}
